package dice.main;

import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import java.util.Date;

import javax.json.JsonNumber;
import javax.json.JsonObject;

import dice.client.web.BeginSessionResponse;
import dice.client.web.DiceResponse;
import dice.client.web.DiceWebAPI;
import dice.client.web.SessionInfo;

public class Play {

	private static final Integer MAX_BETS = 80;
	private static final Integer BETS_CYCLES = 1000;
	private static final Double ONE_BET_IN = 0.5D;

	private static Integer maxBets = MAX_BETS;
	private static BigDecimal m = new BigDecimal("21");
	private static Long guessLow1 = 0L;
	private static Long guessHigh1 = 949999L;
	private static Long guessLow2 = 500500L;
	private static Long guessHigh2 = 999999L;
	private static MathContext mathContext = new MathContext(1, RoundingMode.DOWN);
	private static BigDecimal stopPercent = new BigDecimal("1.05");
	private static BigDecimal p = new BigDecimal("0.0001");
	private static BigDecimal initialBet = new BigDecimal("0.000001");
	private static BigDecimal extraSatoshis = new BigDecimal("0.00003333333333");
	
	private static String apiKey = "a7191396dd5341708cd94773e12f8a4e";
	private static String username = "gtamos";
	private static String password = "gonza6624";


	public static void main(String[] args) throws InterruptedException {

		

		BeginSessionResponse beginSessionResponse = DiceWebAPI.BeginSession(apiKey, username, password);
		SessionInfo session = beginSessionResponse.getSession();
		BigDecimal initialBalance = session.getBalance();
		BigDecimal initialBetBalance = initialBalance;
		System.out.println("------------------------------------------------------");
		System.out.println("STARTING " + BETS_CYCLES*maxBets + " BETS BOT IN "  + BETS_CYCLES + " CYCLES (Estimated time: " + (int) (BETS_CYCLES*maxBets*ONE_BET_IN/60/60) + " hours )" );
		System.out.println("------------------------------------------------------");
		Boolean playALot = false;
		BigDecimal pl = new BigDecimal("0.05");
		Boolean playAllOrNothing = false;
		for (Integer j = 0 ; j < BETS_CYCLES ; j++){
			beginSessionResponse = DiceWebAPI.BeginSession(apiKey, username, password);
			session = beginSessionResponse.getSession();
			System.out.println("START CYCLE# " + (j + 1));
			System.out.println("------------------------------------------------------");
			System.out.println("Initial balance: " + initialBalance);
			Integer win = 0;
			Integer loss = 0;
			Integer maxConsecutiveWins = 0;
			Integer consecutiveWins = 0;
			Integer maxConsecutiveLoss = 0;
			Integer consecutiveLoss = 0;


			BigDecimal newBalance = initialBalance;

			String betType = "Bet# ";
			Integer i;
			Date d0 = new Date();
			//payIn = initialBalance.divide(p).round(mathContext);		
			BigDecimal initialThousands = new BigDecimal("10000");
			initialBet = newBalance.multiply(p);
			initialBet = DiceWebAPI.TruncateSatoshis(initialBet);
			BigDecimal payIn = initialBet;
			BigDecimal maxBet = payIn;
			for(i = 0; i<maxBets ; i++){
				/*if(newBalance.movePointRight(8).compareTo(initialThousands) >= 0){
					initialBalance.add(new BigDecimal("0.00000001"));
					initialThousands.multiply(BigDecimal.valueOf(10D));
				}else if(!initialBet.equals(new BigDecimal("0.00000001"))){
					initialBet.subtract(new BigDecimal("0.00000001"));
					initialThousands.divide(BigDecimal.valueOf(10D));
				}*/
				Date d1 = new Date();
				if(payIn.compareTo(newBalance) > 0){
					payIn = newBalance;
					//break;
				}
				DiceResponse response;
				response = DiceWebAPI.PlaceBet(session, payIn, guessLow1, guessHigh1);
				if(!response.isSuccess()){
					i --;
					//Thread.sleep(500);
					System.out.print ("[NOT SUCESS] ");
					System.exit(0);
				}else{
					System.out.print ("[SUCESS] ");
				}
				Date d2 = new Date();
				newBalance = session.getBalance();			

				System.out.print(betType + (i + 1) + ": Delayed " + (d2.getTime() - d1.getTime()) /1000  + " SEC, Bet " + payIn.toPlainString() + "," );
				JsonObject jsonObject = response.getRawResponse();
				BigDecimal secret = ((JsonNumber) jsonObject.get("Secret")).bigDecimalValue();

				if(guessHigh1.compareTo(secret.longValue()) < 0 && response.isSuccess()){
					System.out.print( " New balance: " + newBalance + " (LOSS)(" + secret + ")");
					consecutiveLoss ++;
					payIn = payIn.multiply(m).add(newBalance.multiply(extraSatoshis));
					/*switch (consecutiveLoss){
					case 1:
						payIn = new BigDecimal("0.00044100");
						break;
					case 2:
						payIn = new BigDecimal("0.01");
						break;
					case 3:
						System.exit(0);
					}
				    */	
				    		
					payIn = DiceWebAPI.TruncateSatoshis(payIn);
					loss++;
					consecutiveWins = 0;
					if(consecutiveLoss > maxConsecutiveLoss){
						maxConsecutiveLoss = consecutiveLoss;
					}
					/*if(consecutiveLoss == 2){
						payIn = newBalance.multiply(p).multiply(m).add(newBalance.multiply(extraSatoshis));
						for(Integer k=0 ; k < consecutiveLoss - 1; k ++){
							payIn = payIn.multiply(m).add(newBalance.multiply(extraSatoshis));		
						}
					}
					*/
					if(consecutiveLoss == 3){
						payIn = newBalance;
					}
					if(i + 1  == maxBets){
						maxBets += 1;
						betType = "(extra) Bet# ";
					}
					if(payIn.compareTo(maxBet) > 0){
						maxBet = payIn;
					}

				}else if(response.isSuccess()){
					System.out.print( " New balance: " + newBalance + " (WIN)(" + secret + ")");
					payIn = initialBet;
					//payIn = newBalance.divide(p).round(mathContext);
					win++;
					consecutiveWins++;
					consecutiveLoss = 0;
					if(consecutiveWins > maxConsecutiveWins){
						maxConsecutiveWins = consecutiveWins;
					}
				}
				System.out.println(" (" + newBalance.subtract(initialBetBalance).toPlainString() + ")");
				initialBetBalance = newBalance;				
			}
			newBalance = session.getBalance();
			System.out.println("");
			System.out.println("");

			System.out.println("--------------------------------------------------RESUME-----------------------------------------------------");
			System.out.println("");
			System.out.println("BETS: " + i); 
			System.out.println("TOTAL TIME: " + (new Date().getTime() - d0.getTime()) / 1000 + " SEC");
			System.out.println("I.BALANCE: " + initialBalance);
			System.out.println("F.BALANCE: " + newBalance.toPlainString());
			System.out.println("MAX BET: " + maxBet.toPlainString());
			System.out.println("WINS: " + win);
			System.out.println("MAX CONSECUTIVE WINS: " + maxConsecutiveWins);
			System.out.println("LOSSES: " + loss);
			System.out.println("MAX CONSECUTIVE LOSSES: " + maxConsecutiveLoss);
			System.out.println("PROFIT: " + newBalance.subtract(initialBalance).toPlainString() + " (" + DiceWebAPI.TruncateSatoshis(newBalance.subtract(initialBalance).multiply(new BigDecimal(100)).divide(initialBalance,RoundingMode.UP)).toPlainString() + "%)");
			System.out.println("");
			System.out.println("-------------------------------------------------------------------------------------------------------------");

			payIn = initialBet;
			initialBalance = newBalance;
			maxBets = MAX_BETS;
			//System.out.println("Wating 10 seconds to next cycle...");
			//Thread.sleep(10000);
			///System.out.println("DONE!!");
			System.out.println("");
		}

	}

}
